#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define KEY 6
char *keyword[KEY] = {"for", "if", "then", "else", "while", "do"};
char id[1024];
int num;
int token;

void lexical(FILE *fp) {
	char x = getc(fp);
    char temp;
    int i=0;
	num = 0;
    while(x==' '||x=='\n'||x=='\t')
    {
        x=getc(fp);
    }
	memset(id,'\0',1024);
	if(x>=65&&x<=90||x>=97&&x<=122)
    {
        do{
            id[i++]=x;
            x=getc(fp);
        }while(x>=65&&x<=90||x>=97&&x<=122||x=='0'||x=='1'||x=='2'||x=='3'||x=='4'||x=='5'||x=='6'||x=='7'||x=='8'||x=='9');
        ungetc(x,fp);
        if(!strcmp(id,keyword[0]))
        {
            token=1;
            strcpy(id,"for");
        }
        else if(!strcmp(id,keyword[1]))
        {
            token=2;
            strcpy(id,"if");
        }
        else if(!strcmp(id,keyword[2]))
        {
            token=3;
            strcpy(id,"then");
        }
        else if(!strcmp(id,keyword[3]))
        {
            token=4;
            strcpy(id,"else");
        }
        else if(!strcmp(id,keyword[4]))
        {
            token=5;
            strcpy(id,"while");
        }
        else if(!strcmp(id,keyword[5]))
        {
            token=6;
            strcpy(id,"do");
        }
        else
        {
            token=10;
        }
    }
    else if(x=='0'||x=='1'||x=='2'||x=='3'||x=='4'||x=='5'||x=='6'||x=='7'||x=='8'||x=='9')
    {
        do{
            num=num*10+x-'0';
            x=getc(fp);
        }while(x=='0'||x=='1'||x=='2'||x=='3'||x=='4'||x=='5'||x=='6'||x=='7'||x=='8'||x=='9');
        ungetc(x,fp);
        token=11;
    }
    else {
	if(x=='+')
    {
        token=13;
        strcpy(id,"+");
    }
    if(x=='-')
    {
        token=14;
        strcpy(id,"-");
    }
    if(x=='*')
    {
        token=14;
        strcpy(id,"*");
    }
    if(x=='/')
    {
        token=15;
        strcpy(id,"/");
    }
    if(x==':')
    {
        temp=getc(fp);
        if(temp=='=')
        {
            token=18;
            strcpy(id,":=");
        }
        else
        {
            token=17;
            strcpy(id,":");
            ungetc(temp,fp);
        }
    }
    if(x=='<')
    {
        temp=getc(fp);
        if(temp=='=')
        {
        token=22;
        strcpy(id,"<=");
        }
        else if(temp=='>')
        {
            token=21;
            strcpy(id,"<>");
        }
        else
        {
            token=20;
            strcpy(id,"<");
            ungetc(temp,fp);
        }
    }
    if(x=='>')
    {
        temp=getc(fp);
        if(temp=='=')
        {
            token=24;
            strcpy(id,">=");
        }
        else{
            token=23;
            strcpy(id,">");
            ungetc(temp,fp);
        }
    }
    if(x=='=')
    {
        token=25;
        strcpy(id,"=");
    }
    if(x==';')
    {
        token=26;
        strcpy(id,";");
    }
    if(x=='(')
    {
        token=27;
        strcpy(id,"(");
    }
    if(x==')')
    {
        token=28;
        strcpy(id,")");
    }
    if(x=='#')
    {
        token=0;
        strcpy(id,"#");
    }

	//process x
	//process each regex here
	//store the result in global variables id, num, or token
	//in case of backtrack, use
	//ungetc(x, fp);
    }
}

int main(int argc, const char *argv[])
{
	if(argc < 2) {
		printf("insufficient args\n");
		return 1;
	}
	FILE *fp = fopen(argv[1], "r");
	do {
		lexical(fp);
		switch(token) {
			case 10: printf("(id, %s)\n", id); break;
			case 11: printf("(num, %d)\n", num); break;
			case -1: printf("error!\n"); break;
			default: printf("(%d, %s)\n", token, id);
		}
	} while(token != 0);


	fclose(fp);
	return 0;
}
